﻿Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Navigation
Imports System
Imports System.Collections.Generic
Imports SilverlightContrib.Controls


Partial Public Class MainPage
    Inherits UserControl

    Public Sub New()
        InitializeComponent()

        Me.loginContainer.Child = New LoginStatus()
    End Sub

    Friend Sub RefreshNavigationMenu()

        Me.PagesDomainDataSource.Load()

    End Sub


    ''' <summary>
    '''     After the Frame navigates, ensure the HyperlinkButton representing the current page is selected
    ''' </summary>
    Private Sub ContentFrame_Navigated(ByVal sender As Object, ByVal e As NavigationEventArgs)
        'For Each child As UIElement In LinksStackPanel.Children
        '    Dim hb As HyperlinkButton = TryCast(child, HyperlinkButton)
        '    If hb IsNot Nothing AndAlso hb.NavigateUri IsNot Nothing Then
        '        If hb.NavigateUri.ToString().Equals(e.Uri.ToString()) Then
        '            VisualStateManager.GoToState(hb, "ActiveLink", True)
        '        Else
        '            VisualStateManager.GoToState(hb, "InactiveLink", True)
        '        End If
        '    End If
        'Next
    End Sub

    ''' <summary>
    '''     If an error occurs during navigation, show an error window
    ''' </summary>
    Private Sub ContentFrame_NavigationFailed(ByVal sender As Object, ByVal e As NavigationFailedEventArgs)
        e.Handled = True
        ErrorWindow.CreateNew(e.Exception)
    End Sub



    Private Sub PagesDomainDataSource_LoadedData(ByVal sender As Object, ByVal e As System.Windows.Controls.LoadedDataEventArgs) Handles PagesDomainDataSource.LoadedData
        'Note: Build navigation menu
        Me.NavigationPanel.Children.Clear()

        Dim items = DirectCast(Me.PagesDomainDataSource.Data, System.Windows.Controls.Ria.DomainDataSourceView)

        For Each item As Data.Page In items

            Dim menuItem As New NavigationMenuItem() _
                With {.DataContext = item, .Margin = New Thickness(5, 3, 0, 0), .TrackingValue = item.Title}

            Me.NavigationPanel.Children.Add(menuItem)

        Next



    End Sub

End Class